<?php
/**
 * Created by PhpStorm.
 * User: caipeichao
 * Date: 14-3-11
 * Time: PM5:41
 */

namespace Admin\Controller;

use Admin\Builder\AdminListBuilder;
use Admin\Builder\AdminConfigBuilder;
use Admin\Builder\AdminSortBuilder;
use Think\Model;
use Weibo\Api\WeiboApi;

class HelpController extends AdminController
{
    protected $mHelpModel;
    protected $mWorkModel;
    protected $mEvaluateModel;

    function _initialize()
    {
        $this->mHelpModel = D('Help');
        $this->mWorkModel = D('HelpWorks');
        $this->mEvaluateModel = D('HelpEvaluate');
        parent::_initialize();
    }

    public function index()
    {

    }

    public function helpList()
    {
        $aTitle = I('get.title','','op_h');
        $aId = I('get.id','','intval');
        $aRecommend = I('get.recommend','','op_t');

        $r = 20;
        $map['status'] =array('egt',0);
        $aTitle &&  $map['title'] = array('like', '%' . $aTitle . '%');
        $aId &&  $map['id'] = $aId;
        ($aRecommend == '1' || $aRecommend =='是') && $map['recommend'] = 1;
        ($aRecommend == '0' || $aRecommend =='否') && $map['recommend'] = 0;
        $list = $this->mHelpModel->where($map)->order('create_time desc')->select();
        $totalCount = $this->mHelpModel->where($map)->count();
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙列表')
            ->setSearchPostUrl(U('helpList'))->search('ID', 'id')->search('标题', 'title')->search('是否头条', 'recommend')
            ->ajaxButton(U('admin/help/setRecommend'), array('is_recommend' => 1), '设为头条')->ajaxButton(U('admin/help/setRecommend'), array('is_recommend' => 0), '取消头条')
            ->setStatusUrl(U('Help/setHelpStatus'))->buttonEnable()->buttonDisable()->buttonDelete()
            ->keyId()->keyLink('title', '标题', 'help/index/detail?id=###')->keyUid('uid', '发布用户')->keyText('sign_count', '报名人数')->keyText('good_count', '好评数')
            ->keyCreateTime()->keyStatus()->keyMap('recommend', '是否头条',array(0=>'否',1=>'是'))->keyDoActionEdit('help/index/ask?id=###')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }


    public function helpRecycle()
    {
        $r = 20;
        $map['status'] =-1;
        $list = $this->mHelpModel->where($map)->order('create_time desc')->select();
        $totalCount = $this->mHelpModel->where($map)->count();
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙回收站')
            ->setStatusUrl(U('Help/setHelpStatus'))->buttonRestore()
            ->keyId()->keyLink('title', '标题', 'help/index/detail?id=###')->keyUid('uid', '发布用户')->keyText('sign_count', '报名人数')->keyText('good_count', '好评数')
            ->keyCreateTime()->keyStatus()->keyDoActionEdit('help/index/ask?id=###')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }
    public function setRecommend($ids = '', $is_recommend = 1)
    {

        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        $this->mHelpModel->where(array('id' => array('in', $ids)))->setField('recommend', $is_recommend);
        if($is_recommend){
            send_top_message($ids);
        }
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);
    }

    public function setHelpStatus($ids, $status)
    {
        $builder = new AdminListBuilder();
        $builder->doSetStatus('Help', $ids, $status);
    }

    public function workList()
    {
        $r = 20;
        $map['status'] =array('egt',0);
        $list = $this->mWorkModel->where($map)->select();
        $totalCount = $this->mWorkModel->where($map)->count();
        $this->parseWork($list);
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙作品列表')
            ->setStatusUrl(U('Help/setWorkStatus'))->buttonEnable()->buttonDisable()->buttonDelete()
            ->keyId()->keyLink('title', '标题', 'help/index/production?id={$help_id}')->keyUid()->keyText('detail', '描述')->keyText('support', '赞')->keyText('oppose', '踩')
            ->keyCreateTime()->keyStatus()->keyDoActionEdit('help/index/publishwork?id=###')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    public function workRecycle()
    {
        $r = 20;
        $map['status'] =-1;
        $list = $this->mWorkModel->where($map)->select();
        $totalCount = $this->mWorkModel->where($map)->count();
        $this->parseWork($list);
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙作品回收站')
            ->setStatusUrl(U('Help/setWorkStatus'))->buttonRestore()
            ->keyId()->keyLink('title', '标题', 'help/index/production?id={$help_id}')->keyUid()->keyText('detail', '描述')->keyText('support', '赞')->keyText('oppose', '踩')
            ->keyCreateTime()->keyStatus()->keyDoActionEdit('help/index/publishwork?id=###')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    public function setWorkStatus($ids, $status)
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);
        M('HelpWorks')->where(array('help_id'=>array('in', $ids)))->save(array('status'=>$status));
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);

    }

    protected function parseWork(&$list)
    {
        foreach ($list as $k => &$v) {
            $v['id'] = $v['help_id'];
        }
        return $list;
    }


    public function complain()
    {
        $r = 20;
        $status = I('status', -1, 'intval');
        $status != -1 && $map['status'] = $status;
        $list = D('HelpComplain')->where($map)->select();
        $totalCount = D('HelpComplain')->where($map)->count();

        foreach ($list as $k => &$v) {
            $help = D('Help')->where('id=' . $v['help_id'])->find();
            $v['help_title'] = $help['title'];

            $attaches = explode(',', $v['attach_ids']);
            $v['images'] = '<div class="popup-gallery" style="width: 800px">';
            foreach ($attaches as $att) {
                $v['images'] .= '<a class="popup" href="' . get_cover($att, 'path') . '"><img style="margin-right:5px;margin-bottom:5px;" src="' . getThumbImageById($att, 100, 100) . '"/></a>';
            }
            $v['images'] .= '</div>';
        }

        $builder = new AdminListBuilder();
        $builder
            ->title('投诉列表')
            ->setStatusUrl(U('help/setComplainStatus'))
            ->button('查看全部', array('href' => U('complain', array('status' => -1))))
            ->button('查看已处理', array('href' => U('complain', array('status' => 1))))
            ->button('查看未处理', array('href' => U('complain', array('status' => 0))))
            ->buttonDisable('', '设为未处理')->buttonEnable('', '设为已处理')
            ->keyId('help_id')->keyLink('help_title', '帮忙标题', 'help/index/detail?id={$help_id}')->keyUid('uid', '投诉人')->keyText('content', '内容')->keyText('images', '证明图片')
            ->keyCreateTime()->key('status', '状态', 'status', array(0 => '未处理', 1 => '已处理'))
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    public function setComplainStatus($ids, $status)
    {
        $builder = new AdminListBuilder();
        $builder->doSetStatus('HelpComplain', $ids, $status);
    }


    public function evaluateList()
    {
        $r = 20;
        $map['status'] =array('egt',0);
        $list = $this->mEvaluateModel->where($map)->select();
        $totalCount = $this->mEvaluateModel->where($map)->count();
        $list = $this->parseEvaluate($list);
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙评价列表')
            ->setStatusUrl(U('Help/setEvaluateStatus'))->buttonEnable()->buttonDisable()->buttonDelete()
            ->keyId()->keyLink('help_title', '帮忙标题', 'help/index/evaluate?id={$help_id}')->keyUid('uid', '评论用户')->keyUid('to_uid', '被评论用户')
            ->keyText('type', '类型')->keyText('content', '评论内容')->keyText('score', '评分')->keyText('images', '现场照')
            ->keyCreateTime()
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    public function evaluateRecycle()
    {
        $r = 20;
        $list = $this->mEvaluateModel->where(array('status' => -1))->select();
        $totalCount = $this->mEvaluateModel->where(array('status' => -1))->count();

        $list = $this->parseEvaluate($list);
        $builder = new AdminListBuilder();
        $builder
            ->title('帮忙评价回收站')
            ->setStatusUrl(U('Help/setEvaluateStatus'))->buttonRestore()
            ->keyId()->keyLink('help_title', '帮忙标题', 'help/index/evaluate?id={$help_id}')->keyUid('uid', '评论用户')->keyUid('to_uid', '被评论用户')
            ->keyText('type', '类型')->keyText('content', '评论内容')->keyText('score', '评分')->keyText('images', '现场照')
            ->keyCreateTime()
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    protected function parseEvaluate(&$list)
    {
        foreach ($list as $k => &$v) {
            $help = D('Help')->where('id=' . $v['help_id'])->find();
            $v['help_title'] = $help['title'];

            $attaches = explode(',', $v['attach_ids']);
            $v['images'] = '<div class="popup-gallery" style="width: 320px">';
            foreach ($attaches as $att) {
                $v['images'] .= '<a class="popup" href="' . get_cover($att, 'path') . '"><img style="margin-right:5px;margin-bottom:5px;" src="' . getThumbImageById($att, 100, 100) . '"/></a>';
            }
            $v['images'] .= '</div>';
            $v['type'] = get_evaluate_type($v['type']);
            $v['score'] = '<p>守时：' . $v['punctual'] . '</p><p>守信：' . $v['trustworthy'] . '</p><p>技能：' . $v['technique'] . '</p><p>态度：' . $v['attitude'] . '</p>';
        }
        return $list;
    }

    public function setEvaluateStatus($ids, $status)
    {
        $builder = new AdminListBuilder();
        $builder->doSetStatus('HelpEvaluate', $ids, $status);
    }

    public function config()
    {
        $admin_config = new AdminConfigBuilder();
        $data = $admin_config->handleConfig();
        $admin_config->title('帮忙设置')
            ->keyText('SIGN_DEADLINE', '报名截止时间', '0为不限制')
            ->keyText('CHOSE_DEADLINE', '选择帮手截止时间', '0为不限制')
            ->keyText('PUBLISH_DEADLINE', '作品上传截止时间', '0为不限制')
            ->keyText('EVALUATE_DEADLINE', '评价截止时间', '0为不限制')
            ->buttonSubmit('', '保存')->data($data);
        $admin_config->display();
    }


}
